﻿using AutoMapper;
using B.S.BaseData.Domain;
using B.S.BaseData.ErrorCode;
using B.S.BaseData.Infratructure.Interfares;
using B.S.BaseData.Read.Api.Application.Command;
using B.S.BaseData.Read.Api.Dtos;
using MediatR;
using System.Collections.Generic;

namespace B.S.BaseData.Read.Api.Application.CommandHander
{
    public class GetRecodeCommandHander : IRequestHandler<GetRecodesCommand, ApiPaging<List<GetRecodeDto>>>
    {

        private readonly IMapper mapper;
        private readonly IBaseRequsitory<WF_Recode> recodeRequsitory;
        private readonly IBaseRequsitory<Leave> leaveRequsitory;
        private readonly IBaseRequsitory<WF_Instafre> instaresRequsitory;

        public GetRecodeCommandHander(IMapper mapper, IBaseRequsitory<WF_Recode> recodeRequsitory, IBaseRequsitory<Leave> leaveRequsitory, IBaseRequsitory<WF_Instafre> instaresRequsitory)
        {
            this.mapper = mapper;
            this.recodeRequsitory = recodeRequsitory;
            this.leaveRequsitory = leaveRequsitory;
            this.instaresRequsitory = instaresRequsitory;
        }

        /// <summary>
        /// 显示审批操作
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<ApiPaging<List<GetRecodeDto>>> Handle(GetRecodesCommand request, CancellationToken cancellationToken)
        {
            ApiPaging<List<GetRecodeDto>> result = new ApiPaging<List<GetRecodeDto>>();
            result.Code = ApiEnum.显示成功;
            result.Msg = ApiEnum.显示成功.ToString();

            var list = from l in leaveRequsitory.GetAllAsync() 
                       join i in instaresRequsitory.GetAllAsync() on l.LeaveId equals i.leaveId
                       //where l.OperatorId == request.OperatorId
                       //||
                       //i.NextOperNo == request.OperatorId.ToString()
                       select new GetRecodeDto
                       {
                           StarterName = i.StarterName,
                           UserId = i.UserId,
                           Instaresid = i.InstafreId,
                           LeaveId = l.LeaveId,
                           LeaveName = l.LeaveName,
                           DepartId = l.DepartId,
                           LeaveType = l.LeaveType,
                           LeaveCreateTime = l.LeaveCreateTime,
                           CoverageTime = l.CoverageTime,
                           TotalHours = l.TotalHours,
                           Cause = l.Cause,
                           DepartmentHeads = l.DepartmentHeads,
                           Appstatus = l.Appstatus
                       };

            result.TotalCount = list.Count();
            result.TotalPage = (int)Math.Ceiling(result.TotalCount * 1.0 / request.PageSize);

            var entity = list.OrderBy(x => x.LeaveId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize);

            result.Data = mapper.Map<List<GetRecodeDto>>(entity.ToList());
            return Task.FromResult(result);
        }
    }
}
